<?php


namespace Tbf\Bundle\SerializerBundle\Register;


use Symfony\Component\DependencyInjection\ContainerInterface;
use Tbf\Bundle\SerializerBundle\Handler\LoadFromArray\LoadFromArrayInterface;
use Tbf\Bundle\SerializerBundle\Handler\NewFromArray\NewFromArrayInterface;
use Tbf\Bundle\SerializerBundle\Handler\ToArray\ToArrayInterface;
/**
 * 有三种类型的序列化接口
 * toArray select
 * newFromArray insert
 * loadFromArray update
 */
class Register {
    protected $toArrayRegister;
    protected $newFromArrayRegister;
    protected $loadFromArrayRegister;
    function __construct(ContainerInterface $container){
        $this->toArrayRegister = new SingleRegister($container);
        $this->newFromArrayRegister = new SingleRegister($container);
        $this->loadFromArrayRegister = new SingleRegister($container);
    }

    /**
     * @param array $map map[string]map[string]string type,classname,service_id
     */
    function registerByServiceIdMap($map){
        foreach($map as $type=>$m1){
            switch($type){
                case 'ToArray':
                    $this->toArrayRegister->registerByMapClassNameServiceId($m1);
                    break;
                case 'NewFromArray':
                    $this->newFromArrayRegister->registerByMapClassNameServiceId($m1);
                    break;
                case 'LoadFromArray':
                    $this->loadFromArrayRegister->registerByMapClassNameServiceId($m1);
                    break;
            }
        }
    }
    function registerToArray($class_name,ToArrayInterface $obj){
        $this->toArrayRegister->register($class_name,$obj);
    }
    function registerNewFromArray($class_name,NewFromArrayInterface $obj){
        $this->toArrayRegister->register($class_name,$obj);
    }
    function registerLoadFromArray($class_name,LoadFromArrayInterface $obj){
        $this->toArrayRegister->register($class_name,$obj);
    }
    function getToArray($class_name){
        return $this->toArrayRegister->getByClassName($class_name);
    }
    function getNewFromArray($class_name){
        return $this->newFromArrayRegister->getByClassName($class_name);
    }
    function getLoadFromArray($class_name){
        return $this->loadFromArrayRegister->getByClassName($class_name);
    }
}